package mz.matafe.bolao

import grails.test.*

class MatchTests extends GrailsUnitTestCase {

    def team1
    def team2
    def match    

    protected void setUp() {
        super.setUp()
        team1 = new Team(name: 'Africa do Sul',
                teamDetails: new TeamDetails(played:0,
                won:0,draw:0,lost:0,goalsFor:0,goalsAgainst:0,points:0))
        team2 = new Team(name: 'Mexico',
            teamDetails: new TeamDetails(played:0,
                won:0,draw:0,lost:0,goalsFor:0,goalsAgainst:0,points:0))
        // Make sure we can invoke validate() on our Team domain object.
        mockDomain(Team, [team1,team2])
        match = new Match(matchNumber: 1, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        mockForConstraintsTests(Match, [match])
    }

    protected void tearDown() {
        super.tearDown()
    }

    /**
     * Testing toString() method
     */
    void testToString() {
        assertEquals '1', match.toString()
    }

    /**
     * Testing the nullable constraints
     */
    void testConstraintsNullable() {
        match = new Match()
        assertFalse match.validate()
        assertEquals 5, match.errors.errorCount
        assertEquals 'The matchNumber is null', 'nullable',
            match.errors['matchNumber']
        assertEquals 'The matchDate is null', 'nullable',
            match.errors['matchDate']
        assertEquals 'The venue is null', 'nullable',
            match.errors['venue']
        assertEquals 'The team1 is null', 'nullable',
            match.errors['team1']
        assertEquals 'The team1 is null', 'nullable',
            match.errors['team2']
        match = new Match(matchNumber: 2, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        if (!match.validate())
            println match.errors
        assertTrue match.validate()
    }

    /**
     * Testing the unique constraints
     */
    void testConstraintsUnique() {
        match = new Match(matchNumber: 1, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        assertFalse match.validate()
        assertEquals 'The matchNumber must be unique','unique', match.errors['matchNumber']
        match = new Match(matchNumber: 2, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        assertTrue match.validate()
    }

    /**
     * Testing the blank constraints
     */
    void testConstraintsBlank() {
        match = new Match(matchNumber: 2, matchDate: new Date()+1,
            venue: '', team1: team1, team2: team2)
        assertFalse match.validate()
        assertEquals 1, match.errors.errorCount
        assertEquals 'The venue is blank','blank', match.errors['venue']
        match = new Match(matchNumber: 2, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        if (!match.validate())
            println match.errors
        assertTrue match.validate()
    }

    /**
     * Testing the validator constraints
     */
    void testConstraintsValidator() {
        match = new Match(matchNumber: 2, matchDate: new Date(),
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        assertFalse match.validate()
        assertEquals 1, match.errors.errorCount
        assertEquals 'The matchDate is before or equals today','validator', match.errors['matchDate']
        match = new Match(matchNumber: 2, matchDate: new Date()+1,
            venue: 'Johannesburg - JSC', team1: team1, team2: team2)
        if (!match.validate())
            println match.errors
        assertTrue match.validate()
    }
}
